package com.ruoyi.web.controller.ism;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ism.domain.IsmActivityPublish;
import com.ruoyi.ism.service.IIsmActivityPublishService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 活动发布Controller
 * 
 * @author ruoyi
 * @date 2024-07-18
 */
@RestController
@RequestMapping("/ism/publish")
public class IsmActivityPublishController extends BaseController
{
    @Value("${ruoyi.profile}")
    private String uploadPath;

    @Autowired
    private IIsmActivityPublishService ismActivityPublishService;

    /**
     * 查询活动发布列表
     */
    @PreAuthorize("@ss.hasPermi('ism:publish:list')")
    @GetMapping("/list")
    public TableDataInfo list(IsmActivityPublish ismActivityPublish)
    {
        startPage();
        List<IsmActivityPublish> list = ismActivityPublishService.selectIsmActivityPublishList(ismActivityPublish);
        return getDataTable(list);
    }

    /**
     * 导出活动发布列表
     */
    @PreAuthorize("@ss.hasPermi('ism:publish:export')")
    @Log(title = "活动发布", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, IsmActivityPublish ismActivityPublish)
    {
        List<IsmActivityPublish> list = ismActivityPublishService.selectIsmActivityPublishList(ismActivityPublish);
        ExcelUtil<IsmActivityPublish> util = new ExcelUtil<IsmActivityPublish>(IsmActivityPublish.class);
        util.exportExcel(response, list, "活动发布数据");
    }

    /**
     * 获取活动发布详细信息
     */
    @PreAuthorize("@ss.hasPermi('ism:publish:query')")
    @GetMapping(value = "/{activityId}")
    public AjaxResult getInfo(@PathVariable("activityId") Long activityId)
    {
        return success(ismActivityPublishService.selectIsmActivityPublishByActivityId(activityId));
    }

    /**
     * 新增活动发布
     */
    @PreAuthorize("@ss.hasPermi('ism:publish:add')")
    @Log(title = "活动发布", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody IsmActivityPublish ismActivityPublish) throws IOException, WriterException {
        String activitySn = "hd"+ DateUtils.dateTimeNow();
        ismActivityPublish.setActivitySn(activitySn);
        ismActivityPublish.setCreateBy(getUsername());
        String sqrcode ="/profile/upload//qrCode/"+activitySn+"s.PNG";
        String eEqrcode ="/profile/upload//qrCode/"+activitySn+"e.PNG";
        drawQrCode(activitySn+"s",300,300);
        drawQrCode(activitySn+"e",300,300);
        ismActivityPublish.setActivitySqrcode(sqrcode);
        ismActivityPublish.setActivityEqrcode(eEqrcode);
        return toAjax(ismActivityPublishService.insertIsmActivityPublish(ismActivityPublish));
    }

    /**
     * 修改活动发布
     */
    @PreAuthorize("@ss.hasPermi('ism:publish:edit')")
    @Log(title = "活动发布", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody IsmActivityPublish ismActivityPublish) throws IOException, WriterException {
        ismActivityPublish.setUpdateBy(getUsername());
        String activitySn = ismActivityPublish.getActivitySn();
        String sqrcode ="/profile/upload//qrCode/"+activitySn+"s.PNG";
        String eEqrcode ="/profile/upload//qrCode/"+activitySn+"e.PNG";
        drawQrCode(activitySn+"s",300,300);
        drawQrCode(activitySn+"e",300,300);
        ismActivityPublish.setActivitySqrcode(sqrcode);
        ismActivityPublish.setActivityEqrcode(eEqrcode);
        return toAjax(ismActivityPublishService.updateIsmActivityPublish(ismActivityPublish));
    }

    /**
     * 删除活动发布
     */
    @PreAuthorize("@ss.hasPermi('ism:publish:remove')")
    @Log(title = "活动发布", businessType = BusinessType.DELETE)
	@DeleteMapping("/{activityIds}")
    public AjaxResult remove(@PathVariable Long[] activityIds)
    {
        return toAjax(ismActivityPublishService.deleteIsmActivityPublishByActivityIds(activityIds));
    }

    private void drawQrCode(String content, int width, int height) throws WriterException, IOException {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix bitMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height);
        Path path = Paths.get(uploadPath+"/upload/qrCode/"+content+".PNG");
        MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
    }
}
